import * as THREE from "three";
import { Config } from "./Config";
import { GLTFLoader } from "three/examples/jsm/loaders/GLTFLoader";

export class Coin extends THREE.Object3D {
  mesh: THREE.Mesh = null as any;
  constructor() {
    super();
  }

  init(mesh: THREE.Mesh) {
    this.mesh = mesh;
    this.add(this.mesh);
  }

  static load() {
    return new Promise<void>(resolve => {
      // 加载 GLTF 模型
      const loader = new GLTFLoader();
      loader.load(
        './3d/coin/scene.gltf', // 替换为您的 GLTF 模型路径
        (gltf) => {
          const model = gltf.scene;
          console.log("model", model)

          model.traverse((child) => {
            if (child instanceof THREE.Mesh) {
              // 在这里对每个 Mesh 进行操作
              console.log(child);
              // 计算模型的包围盒
              const box = new THREE.Box3().setFromObject(child);
              const size = box.getSize(new THREE.Vector3());

              console.log("size", size, "box", box)

              // 计算缩放因子
              const maxDim = Math.max(size.x, size.y, size.z);
              const scale = 30 / maxDim;

              console.log("scale", scale, child.position)

              // 应用缩放
              child.scale.set(scale, scale, scale);
              child.rotation.x = 90 * (Math.PI / 180);

              const coin = new Coin();
              coin.init(child);
              Config.coin = coin;
              resolve();
            }
          });


        }
      );
    })

  }
}